package com.wj.chatroom.mapper;

import com.wj.chatroom.model.Message;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
@Mapper
public interface MessageMapper {
    //获取最后一条消息
    @Select("select content from message where sessionId = #{sessionId} order by postTime  desc " +
            " limit 1")
    String getLastMessageBySessionId(int sessionId);
    //获取历史消息(有的历史消息可能特别多，此处做出限制，默认只取最近的100条数据)
    @Select("select messageId, fromId, username as fromName, sessionId, content from user,message " +
            "where user.userId = message.fromId and message.sessionId =#{sessionId}" +
            " order by postTime desc limit 100")
    List<Message> getMessageBySessionId(int sessionId);
    @Insert("insert into message values(null,#{fromId},#{sessionId},#{content},now())")
    void add(Message message);
}